<template>
	<gracePage :customHeader="false">
		<!-- 页面主体 -->
		<view slot="gBody" class="grace-body">
			<graceEmptyNew v-if="empty">
				<view slot="img" class="empty-view">
					<!-- 请根据您的项目要求制作并更换为空图片 -->
					<image class="empty-img" mode="widthFix" src="/static/empty.png"></image>
				</view>
				<text slot="text" class="grace-text-small grace-gray">抱歉，没有搜索到任何数据</text>
			</graceEmptyNew>
			<view v-if="!empty" class="grace-list" v-for="(item, index) in addressList" :key="index">
				<view class="grace-list-items" @click="bind(item)">
					<view class="grace-list-body grace-border-b">
						<view class="grace-list-title">
							<text class="grace-h3">{{item.hardwareField5}}</text>
						</view>
						<view class="grace-list-body-desc">{{item.hardwareField2}}</view>
					</view>
					<text class="grace-list-arrow-right grace-icons icon-arrow-right"></text>
				</view>
			</view>
		</view>
	</gracePage>
</template>
<script>
	import gracePage from "../../graceUI/components/gracePage.vue";
	import graceEmptyNew from "../../graceUI/components/graceEmptyNew.vue";
	var _self; 
	export default {
		data() {
			return {
				empty : true,
				source: 0,
				addressList: [],
				// addressList: [
				// 	{
				// 		field1: 'field7',
				// 		field2: '王鑫',
				// 		field3: '18666666666',
				// 		field4: '长安大学',
				// 		field5: '陕西省西安市东城区',
				// 		field6: 'B区',
				// 		field7: true
				// 	},{
				// 		field1: 'field8',
				// 		field2: '王鑫',
				// 		field3: '18667766666',
				// 		field4: '西安工业大兴',
				// 		field5: '陕西省西安市西城区',
				// 		field6: '西单元302',
				// 		field7: false,
				// 	}
				// ],
				userInfoData : "",
				object : "",
				token:"",
				pageNum : 1,
				//每次加载的记录数
				pageSize : 10,
				userID :"",
				from:"",//打开此页面来源
				phone:"",
				
			}
		},
		async onLoad(e){
			_self = this;
			//获取token，同步方法
			_self.token =  await _self.$myFunction.getToken();
			_self.token = _self.token.token;
			_self.userInfoData = _self.$myFunction.appStorage('wuLuUserInfo');
			// 获取详情
			_self.userID = e.id;			
			try{
				_self.from = e.from;
			}catch(e){
				//TODO handle the exception
			}
			// _self.userID = 3;
			_self.getObjectDetail();
		},
		onShow:function(e){
			uni.$on('addressAddRefresh',function(data){
				_self.getObjectDetail();
			})
		},
		methods: {
			addAddress(type, item){
				uni.navigateTo({
					url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}&id=${_self.userID}`
				})
			},
			//添加或修改成功之后回调
			refreshList(data, type){
				//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
				this.addressList.unshift(data);
				
				// console.log(data, type);
			},
			getObjectDetail: function(){
				_self.userInfoData = _self.$myFunction.appStorage('wuLuUserInfo');
				var params = {
					'field1':'',
					'field2':_self.userInfoData.field2,
					'field3':'',
					'field4':'',
					'field5':'',
					'field6':'',
					'field7':'',
					'field8':'',
					'field9':'',
					'field10':'',
					'field11':'',
					'field12':'',
					'field13':'',
					'field14':'',
					'field15':'',
					'field16':'',
					'field17':'油箱卫士',
					'field18':'',
					'field19':'',
					'field20':'',
					'field21':'',
					'status':"1",
					'createTimeFrom':'',
					'createTimeTo':'',
					'pageNum':'1',
					'pageSize':'10',
					'order':'asc',
					'field':"createTime"
				};		
				var paramsData = {
					"jsonParas":_self.$AesUtil.encrypt(encodeURIComponent(JSON.stringify(params)))
				}
				//重新获取一次token，防止接口返回认证失败系统重新获取的token未被使用
				_self.token = _self.$myFunction.appStorage('wuLuToken').token;
				uni.showToast({
					title: '请稍后...',
					duration:_self.$myFunction.appConfig().requestWaitTime,
					icon: 'none',
				});
				uni.request({
					url:  _self.$myFunction.appConfig().serverAddress+'normalUserHardwareList',
					data: paramsData,
					method : "POST",
					header : {
						'content-type':'application/x-www-form-urlencoded',
						'Authentication':_self.token
					},					
					success: data => {
						uni.hideToast();
						_self.$myFunction.displayLog("normalUserHardwareList服务器返回数据："+JSON.stringify(data));
						if (data.data.code == 200) {
							let count = data.data.data.total;
							if(count>0){
								_self.object = data.data.data.rows;
								_self.addressList.splice(0);
								for(let i=0;i<_self.object.length;i++){
									_self.addressList.push(_self.object[i]);
								}
								if(_self.addressList.length == 0){
									_self.empty = true;
								}else{
									_self.empty = false;
								}
							}else{
								_self.$myFunction.displayLog("服务器返回数据为空");
							}
						}else{
							//如果请求数据因token认证失败，则清空缓存token
							if(data.data.message.indexOf("接口访问未认证") != -1){
								uni.setStorage({
									key: 'wuLuToken',
									data: "",
									success: function () {	
										_self.$myFunction.displayLog("清空缓存token");
										//触发重新获取token
										_self.$myFunction.getToken();
										// 关闭所有页面，打开到应用内的某个页面。
										_self.$myFunction.displayLog("重加载当前页面");
										uni.reLaunch({
											url: '/pages/address/address'
										});
									}
								});
							}else{
								uni.showToast({title: "发生错误，请重试",icon: 'none'});
							}	
						}
					},
					fail: (data, code) => {
						uni.hideToast();
						uni.showToast({title: '连接失败...',icon: 'none'});
					}					
				});				
			},
			bind(item){
				var wuLuCode = item.field5;
				// 缓存数据
				uni.setStorage({
					key: 'wuLuCode',
					data: wuLuCode,
					success: function() {
						_self.$myFunction.displayLog("wuLuCode缓存成功");
						// uni.$emit('wuLuCodeRefresh', {
						// 	msg: '页面刷新'
						// });
						// uni.navigateBack();
						try {
							uni.removeStorageSync('wuLuAppInfo');
							uni.reLaunch({
								url: '/pages/object/index'
							});
						} catch (e) {
							// error
						}
					}
				});			
			}
		},
		components:{
			gracePage, graceEmptyNew
		}
	}
</script>
<style>
.empty-view{width:280rpx; height:280rpx; border-radius:280rpx; background-color:#F6F7F8; margin-top:300rpx;}
.empty-img{width:220rpx; height:200rpx; margin:40rpx; border-radius:200rpx;}
.grace-text-small{margin-top:10px;}
</style>
